var grading = {
	init : function() {
		grading.bindEvent();
		var param = {};
		param.ruleId = $('#ruleId').val();
		grading.initTree(param);
	},
	bindEvent : function() {
		/**
		 * 添加
		 */
		$('#add').unbind('click').bind('click', function() {
			var opt = {
				url : baseURL + "/grading/rule/edit",
				title : "新增规则",
				width : 400,
				height : 200,
				confirm : function() {
					grading.ruleSave();
				}
			};
			common.dialog(opt);
		});
		/**
		 * 添加数据
		 */
		$('#addData').unbind('click').bind('click', function() {
			var param = {};
			param.ruleId = $('#ruleId').val();
			param.parentId = $('#parentId').val();
			grading.editData(param);
		});
		/**
		 * 保存
		 */
		$('#save').unbind('click').bind('click', function() {
			grading.save();
		});
		//删除
		$('#del').unbind('click').bind('click', function() {
			var id = $('#editdata').find('input[name="id"]').val();
			common.confirmDialog('确认删除？',function() {
				grading.batchDel(id);
			});
		});
		//删除规则
		$('#delRule').unbind('click').bind('click', function() {
			var id = $('#ruleId').val();
			common.confirmDialog('确认删除？',function() {
				grading.delRule(id);
			});
		});
		$('#deleteRule').unbind('click').bind('click', function() {
			var id = $('#ruleId').val();
			common.confirmDialog('确认删除？',function() {
				grading.delRule(id);
			});
		});
		/**
		 * 清楚
		 */
		$('#cancel').unbind('click').bind('click', function() {
			var id = $('#editdata').find('input[name="id"]').val();
			if(id == null || id == "") {
				$('#type').find('span').html('')
				$('#editdata').find('input[type="text"]').val("");
				$('#editdata').find('input[type="hidden"]').val("");
			} else {
				$('#editdata').find('input[type="text"]').val("");
				$('#editdata').find('input[type="hidden"]').not("input[name='id']").not("input[name='parentId']").val("");
			}
		});
		/**
		 * 选择父
		 */
		$('#parent').unbind('click').bind('click', function() {
			var ruleId = $('#ruleId').val();
			opt = {
				url : baseURL + "/grading/tree",
				param : {type : 6, ruleId : ruleId, isParent : 1},
				title : "请选择",
				leftTitle : "",
				rightTitle2 : "上级",
				rightText : "已选",
				cache : false,
				width : 680,
				height : 480,
				single : true, // 是否单选，默认单选
				canSelectRoot : true,
				onlyLeafSelect : false,
				confirm : function(data){
					$('#editdata').find('input[name="parent"]').val(data.text);
					$('#editdata').find('input[name="parentId"]').val(data.id);
					grading.getTypes(data.id,data.nodes.topId);
				} // 回调函数，返回选中的节点
			}
		  	common.tree(opt);
		});
		/**
		 * 同步
		 */
		$('#sync').unbind('click').bind('click', function() {
			$.ajax({
				type : "post",
				url : baseURL + "/grading/sync",
				dataType : "json",
				success : function(json){
					if(json.success) {
						common.showTip({text:"操作成功！",success:true, autoclose : false, callback : function() {
//								var param = {};
//								param.ruleId = $('#ruleId').val();
//								grading.initTree(param);
								window.location.reload();
							}
						});
					} else {
						common.showTip({text:"操作失败！",success:false});
					}
				}
			});
		});
		/**
		 * 查询
		 */
		$('#search').unbind('click').bind('click', function() {
			var param = {};
			param.poolName = $('#serachPoolName').val();
			param.lunType = $('#serachType').val();
			grading.initTree(param);
		});
		/**
		 * 修改规则
		 */
		$('#ruleId').unbind('change').bind('change', function() {
			var param = {};
			param.ruleId = $(this).val();
			grading.initTree(param);
			grading.edit($(this).val());
		});
		/**
		 * 编辑数据
		 */
		$('#editdata').find('select[name="typeId"]').unbind('change').bind('change', function() {
			var type = $(this).val();
			if(type == '0') {
				$('#name').html('').html($('#osdListHtml').html());
			} else {
				$('#name').html('').html('<input type="text" class="form-control" value="" name="name" />');
			}
		});
		/**
		 * 提交
		 */
		$('#submit').unbind('click').bind('click', function() {
			var opt = {
				url : baseURL + "/grading/rule/edit",
				title : "请选择规则类型",
				width : 400,
				height : 280,
				param : {id : $('#ruleId').val()},
				confirm : function() {
					grading.submit();
				}
			};
			common.dialog(opt);
		});
	},
	/**
	 * 规则保存
	 */
	ruleSave : function() {
		$.ajax({
			type : "post",
			url : baseURL + "/grading/save",
			data : $('#ruleDialog').serialize(),
			dataType : "json",
			success : function(json){
				if(json.success) {
					$('#ruleId').append("<option value='" + json.rule.id + "'>" + json.rule.name + "(未提交)</option>");
					$('#ruleId').val(json.rule.id);
					$('#submit').show();
					common.showTip({text:"操作成功！",success:true, autoclose : false, callback : function() {
							var param = {};
							param.ruleId = $('#ruleId').val();
							grading.initTree(param);
							grading.editData(param);
						}
					});
				} else {
					common.showTip({text:"操作失败！" + json.message,success:false});
				}
			}
		});
	},
	/**
	 * 提交
	 * @param {} id
	 */
	submit : function() {
		$.ajax({
			type : "post",
			url : baseURL + "/grading/submit",
			data : $('#ruleDialog').serialize(),
			dataType : "json",
			success : function(json){
				if(json.success) {
					common.showTip({text:"操作成功！",success:true, autoclose : false});
				} else {
					common.showTip({text:"操作失败！" + json.message,success:false});
				}
			}
		});
	},
	/**
	 * 初始化镜像树
	 */
	initTree : function(param) {
		var param = param || {};
		$.ajax({
			type : "get",
			url : baseURL + "/grading/tree",
			data : param,
			dataType : "json",
			success : function(json){
				$('#tree').tree({
	//				url: baseURL + "/image/tree",
					data : json,
					checkbox: false,
					animate : true,
					cascadeCheck : false,
					// 单击展开
					onClick : function(node){
						$(this).tree("expand", node.target);
						var id=node.id;
						if(node.status) {
							if(node.outype == "0") {
								$('#parentId').val(node.pid);
							} else {
								$('#parentId').val(id);
								$('#parentId').attr('data', node.text);
							}
							var param = {};
							param.id = id;
							param.ruleId = $('#ruleId').val();
							grading.editData(param);
						}
					},
					onCheck:function(node, checked){
						/*var attr = {
								code: node.attributes.ext1 || node.attributes.code,
								codes: node.attributes.ext1 || node.attributes.codes,
								localeName: node.attributes.locale || node.attributes.localeName,
								localeNames: node.attributes.locale || node.attributes.localeNames,
								zhnames: node.attributes.name || node.attributes.zhnames,
								ennames: node.attributes.nameEn || node.attributes.ennames,
								externalCode: node.attributes.externalCode
						};
						// 更新选中节点属性
						$("#easytree" + t).tree("update", {
							target: node.target,
							attributes: addCheckPath("#easytree" + t, node.target, attr, o.pathSpliter)
						});*/
					},
					onLoadSuccess: function(node, data) {
						//设置纵向滚动条，IE8会不能出现滚动条
						/*$("#easytree" + t).parent().css({'overflow-y':'scroll'});
						//远程检索
						var searchUrl = url.replace('/dic/tree', '/dic/tree/search');
						var opt = {
								callback:function(data){
									var $tree = $("#easytree" + t);
									var node = $tree.tree('find', data.id);
									$tree.tree('select', node.target);
									$tree.tree('expandTo', node.target);
						}};
						autocompleteRemortDicTree($('#treediv' + t).find(":input[name=keyword]"), searchUrl, opt);*/
					}
				});
				//清空详情页面
//				$('#info').html('');
			}
		});
	},
	/**
	 * 编辑规则
	 * @param {} id
	 */
	edit : function(id) {
		$.ajax({
            type : "post",
            url : baseURL + "/grading/edit",
            data : {id : id},
            dataType : "html",
            success : function(resultHtml) {
                $("#info").empty();
                $("#info").html(resultHtml);
                if($('#isDraft').val()) {
	                $('#submit').show();
	                $('#delRule').show();
                } else {
	                $('#submit').hide();
	                $('#delRule').hide();
                }
            }
        });
	},
	/**
	 * 编辑数据
	 * @param {} param
	 */
	editData : function(param) {
		var params = param || {};
		$.ajax({
            type : "post",
            url : baseURL + "/grading/data/edit",
            data : {id : params.id, ruleId : params.ruleId, parentId : params.parentId},
            dataType : "html",
            success : function(resultHtml) {
                $("#info").empty();
                $("#info").html(resultHtml);
                //添加表单验证样式
		        $('#parent').validatebox({
					required: true,
					missingMessage: "不能为空"
				});
            }
        });
	},
	/**
	 * 保存
	 */
	save : function() {
		var options ={
//            url : baseURL + '/grading/save',   
            type : 'post',                    
            data : {},
            beforeSubmit : function() {
            },
			success : function(data){
				if(data.success) {
					common.showTip({text:"操作成功！",success:true, autoclose : false, callback : function() {
							var param = {};
							param.ruleId = $('#ruleId').val();
							grading.initTree(param);
							$('#editdata').html('');
						}
					});
				} else {
					common.showTip({text:"操作失败！" + data.message,success:false, autoclose : false});
				}
            }   
		};
		var v = $('#editdata').form('validate');
    	if(v) {
			$('#editdata').ajaxSubmit(options);
    	}
	},
	/**
	 * 批量删除
	 * @param {} ids
	 * @param {} force
	 */
	batchDel : function(ids) {
		$.ajax({
			type:"post",
			url: baseURL + "/grading/batchDel",
			data: {ids : ids},
			dataType:"json" ,
			success :function(data){
				if(data){
					if(data.success){
						common.showTip({text:"操作成功！",success:true});
						var param = {};
						param.ruleId = $('#ruleId').val();
						grading.initTree(param);
						$('#editdata').html('');
					}else{
						common.showTip({text:"操作失败！" + data.message,success:false});
					}
				}
		   }
		});
	},
	delRule : function(id) {
		$.ajax({
			type:"post",
			url: baseURL + "/grading/delRule",
			data: {id : id},
			dataType:"json" ,
			success :function(data){
				if(data){
					if(data.success){
						common.showTip({text:"操作成功！",success:true,callback : function() {
							window.location.reload();
						}});
					}else{
						common.showTip({text:"操作失败！" + data.message,success:false});
					}
				}
		   }
		});
	},
	/**
	 * 获取类型
	 * @param {} id
	 * @param {} ruleId
	 */
	getTypes : function(id, ruleId) {
		$.ajax({
			type:"post",
			url: baseURL + "/grading/getTypes",
			data: {parentId : id, ruleId : ruleId},
			dataType:"json" ,
			success :function(data){
				if(data){
					if(data.success){
						var typeHtml = new Stringbuilder();
						$.each(data.typeList, function (n, value) {
			                typeHtml.append('<option value="' + value.id + '">' + value.name + '</option>');
			            });
			            $('#editdata').find('select[name="typeId"]').html('').html(typeHtml.toString());
			            $('#name').html('').html($('#osdListHtml').html());
					}else{
						common.showTip({text:"获取类型失败！",success:false});
					}
				}
		   }
		});
	}
	
}